//
//  VisitView.swift
//  WeiBo
//
//  Created by 江潘 on 15/6/24.
//  Copyright © 2015年 江潘. All rights reserved.
//

import UIKit

// 设置访问视图协议
protocol VisitViewDelegate:NSObjectProtocol {
    func visitViewClickRegisterBtn()
    func visitViewClickLoginBtn()
}

// 自定义游客登录view
class VisitView: UIView {
    
    // 设置代理属性，并且让代理属性遵守协议
    weak var delegate:VisitViewDelegate?
    
    func setupVisitInfo(isHome:Bool , iconName:String , message:String){
        messageLabel.text = message
    
        if isHome {
            homeImgView.hidden = false
            iconView.image = UIImage(named: "visitordiscover_feed_image_smallicon")
            
        } else {
            homeImgView.hidden = true
            iconView.image = UIImage(named: iconName)
            sendSubviewToBack(maskImgView)
        }
    }
    
    func startAnimation(){
    
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        animation.toValue = M_PI * 2
        
        animation.repeatCount = MAXFLOAT
        
        animation.duration = 40
        
        iconView.layer.addAnimation(animation, forKey: nil)
    }
    
    func register(){
    
        delegate?.visitViewClickRegisterBtn()
    }
    
    func login(){
    
       delegate?.visitViewClickLoginBtn()
    }
   
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI(){
        addSubview(iconView)
        addSubview(maskImgView)
        addSubview(homeImgView)
        addSubview(messageLabel)
        addSubview(registerBtn)
        addSubview(loginBtn)
        
    // 自动布局
        iconView.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: -40))
        
        homeImgView.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: homeImgView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeImgView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0))
        
        registerBtn.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 20))
        
        loginBtn.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 20))
        
        // vfl语言设置自动布局
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[subview(240)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subview" : messageLabel]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[subview(40)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subview" : messageLabel]))
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[subview(100)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subview" : registerBtn]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[subview(40)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subview" : registerBtn]))
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[subview(100)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subview" : loginBtn]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[subview(40)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subview" : loginBtn]))
        
        // 设置背景图和visitview的关系
        
        maskImgView.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[subview]|", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subview" : maskImgView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[subview]-160-|", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subview" : maskImgView]))
        
        // 设置背景颜色
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)

    }
    
// lazyload 子视图
    
    lazy var maskImgView:UIImageView = {
        let image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        return UIImageView(image: image)
    }()

    lazy var iconView:UIImageView = {
        let image = UIImage(named: "visitordiscover_feed_image_smallicon")
        return UIImageView(image: image)
    }()
    
    lazy var homeImgView:UIImageView = {
        let image = UIImage(named: "visitordiscover_feed_image_house")
        return UIImageView(image: image)

    }()
    
    lazy var messageLabel:UILabel = {
        let msglabel = UILabel(frame: CGRectMake(0, 0, 240, 40))
        
         msglabel.text = "登录后，最新、最热微博尽在掌握，不再会与实事潮流擦肩而过"
         msglabel.textAlignment = NSTextAlignment.Center
         msglabel.numberOfLines = 2
         msglabel.font = UIFont.systemFontOfSize(14)
         msglabel.preferredMaxLayoutWidth = 224
         msglabel.sizeToFit()
        
         return msglabel
    }()
    
    lazy var registerBtn:UIButton = {
    
        let btn = UIButton(frame: CGRectMake(0, 0, 100, 40))
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.addTarget(self, action: "register", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
    }()
    
    lazy var loginBtn:UIButton = {
        let btn = UIButton(frame: CGRectMake(0, 0, 100, 40))
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        btn.addTarget(self, action: "login", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
}
